/**
 * 地块类(每块土地一个对象)
 */

class Field extends Laya.Sprite {
    private index : number;
    private cropId : number;
    private imgField = new Laya.Image();
    private imgCrop = new Laya.Image();
    private imgBubble = new Laya.Image();
    private imgBubbleType = new Laya.Image();
    private propBubble = new Laya.Image();
    private propBubbleType = new Laya.Image();
    private strBubbleName = "";
    private imgFrame = new Laya.Image();

    constructor() {
        super();
        this.index = 0;
        this.cropId = 0;
        this.imgCrop.anchorX = 0.5;
	    this.imgCrop.anchorY = 1;
        this.imgBubble.anchorX = 0.5;
	    this.imgBubble.anchorY = 0.5;
        this.imgBubbleType.anchorX = 0.5;
	    this.imgBubbleType.anchorY = 0.5;
        this.imgFrame.visible = false;
        this.imgField.on(Laya.Event.CLICK, this, this.OnFieldClick);
    }

    Init(fieldData:any) : void {
        this.index = fieldData.index;
        this.cropId = fieldData.cropId;

        let fieldBgName = "fieldbg" + (fieldData.index).toString();
        let fieldBg = Game.viewFarm.boxField.getChildByName(fieldBgName) as Laya.Image;
        if (null !=  fieldBg) {
            //土壤
            this.imgField.loadImage("main/field1.png", 0, 0);
            fieldBg.addChild(this.imgField);
            //农作物
            if (null != fieldData.cropId) {
                //加载当前阶段对应的图片
                let cropUrl = "crop/" + Cfg.ItemCfg[fieldData.cropId].skin + fieldData.stage.toString() + ".png";
                this.imgCrop.loadImage(cropUrl);
                //计算农作物根部的坐标
                let cropX = Math.floor(fieldBg.width/2);
				let cropY = Math.floor(fieldBg.height*5/7);
                if (fieldData.stage == 1) {
                    cropY = Math.floor(fieldBg.height*4/7);
                }
                this.imgCrop.pos(cropX, cropY);
                //显示农作物
                this.imgField.addChild(this.imgCrop);

                if (fieldData.stage > Cfg.ItemCfg[this.cropId].stage) {
                    this.AddBubble("harvest");
                }
                else {
                    this.AddDisaster(fieldData);
                }
                //点击地块显示相应作物数据
                this.imgCrop.on(Laya.Event.CLICK,this,this.showCropTips,[this.imgField,this.index]);
            }
        }
    }

    //种植
    Plant(cropId:number) : void {
        this.cropId = cropId;
        //加载图片
        let curStage = 1;
        let cropUrl = "crop/" + Cfg.ItemCfg[cropId].skin + curStage.toString() + ".png";
        this.imgCrop.loadImage(cropUrl);
        //计算农作物根部的坐标
        let cropX = Math.floor(this.imgField.width/2);
        let cropY = Math.floor(this.imgField.height*4/7);
        this.imgCrop.pos(cropX, cropY);
        //显示农作物
        this.imgField.addChild(this.imgCrop);
        //点击地块显示相应作物数据
        this.imgCrop.on(Laya.Event.CLICK,this,this.showCropTips,[this.imgField,this.index]);
        //隐藏边框
        this.HideFrame();
    }

    //改变农作物状态
    ChangeState(stage:number) : void {
        this.imgCrop.graphics.clear();
        //加载当前阶段对应的图片
        let cropUrl = "crop/" + Cfg.ItemCfg[this.cropId].skin + stage.toString() + ".png";
        this.imgCrop.loadImage(cropUrl);
        //检测是否已经成熟
        if (stage > Cfg.ItemCfg[this.cropId].stage) {
            this.AddBubble("harvest");
        }
    }

    //添加气泡
    AddBubble(bubbleName:string) : void {
        if (!PlayerModel.IsSelfFarm() && bubbleName == "harvest" && FieldModel.IsStealed(this.index,KBEngine.app.player().dbid)) {
            //如果已经偷过此农作物，则不再显示收获图标
            return;
        }
        if (bubbleName == "harvest" && this.strBubbleName == Cfg.DisasterCfg[1].skin) {
            //如果农作物成熟，则不显示金土地
            this.imgField.graphics.clear();
            this.imgField.loadImage("main/field1.png");
        }

        this.ClearBubbleMouse();
        this.imgBubbleType.graphics.clear();
        this.imgBubble.graphics.clear();
        this.imgBubble.loadImage("main/bubble.png");

        let cropX = Math.floor(this.imgField.width/2);
        let cropY = -70;
        this.imgBubble.pos(cropX, cropY);
        this.imgField.addChild(this.imgBubble);

        this.strBubbleName = bubbleName;
        let bubbleTypeUrl = "main/bubble_" + bubbleName + ".png";
        this.imgBubbleType.loadImage(bubbleTypeUrl);

        let cropX2 = Math.floor(this.imgBubble.width/2);
        let cropY2 = Math.floor(this.imgBubble.height/2);
        this.imgBubbleType.pos(cropX2, cropY2);
        this.imgBubble.addChild(this.imgBubbleType);
        this.AddBubbleMouse();
    }

    //点击气泡
    OnBubbleClick() : void {
        if (this.strBubbleName) {
            this.ClearBubbleMouse();
            Laya.timer.once(2000, this, this.AddBubbleMouse);
            CtrlManager.GetCtrl(CtrlNames.Field).OnBubbleClick(this.index);
        }
    }

    //气泡的鼠标移动事件处理
    OnBubbleMouseMove() {
        if (this.strBubbleName) {
            this.ClearBubbleMouse();
            Laya.timer.once(2000, this, this.AddBubbleMouse);
            this.OnBubbleClick();
        }
    }

    //给气泡添加鼠标点击事件、滑动事件
    AddBubbleMouse() {
        if (this.strBubbleName) {
            this.imgBubble.on(Laya.Event.CLICK, this, this.OnBubbleClick);
            this.imgBubble.on(Laya.Event.MOUSE_MOVE, this, this.OnBubbleMouseMove);
        }
    }

    //清除气泡的鼠标点击事件、滑动事件
    ClearBubbleMouse() {
        this.imgBubble.offAll(Laya.Event.CLICK);
        this.imgBubble.offAll(Laya.Event.MOUSE_MOVE);
    }

    //移除气泡
    RemoveBubble() {
        this.ClearBubbleMouse();
        this.imgBubble.graphics.clear();
        this.imgBubbleType.graphics.clear();
        this.imgBubble.removeSelf();
    }

    ClearItems() {
        this.ClearBubbleMouse();
        this.imgCrop.graphics.clear();
        this.imgBubble.graphics.clear();
        this.imgBubbleType.graphics.clear();
        this.strBubbleName = "";

        this.imgCrop.removeSelf();
        this.imgBubble.removeSelf();
        this.imgField.graphics.clear();
        this.imgField.loadImage("main/field1.png");
        this.imgFrame.visible = false;
        this.imgFrame.graphics.clear();
    }

    RemoveField() {
        this.ClearItems();
        this.imgField.removeSelf();
    }

    //收获
    Harvest() : void {
        this.cropId = 0;
        this.ClearItems();

        let initPoint = new laya.maths.Point(this.imgField.width/3,this.imgField.height/4);
        initPoint = ComFun.pointChange(this.imgField, Game.viewFarm, initPoint);
        Game.gainAward.playAni(initPoint.x, initPoint.y, Game.viewFarm.imgScore.x, Game.viewFarm.imgScore.y,Game.viewFarm, "item/jifen.png", false);
    }

    //偷取成功,服务器通知
    stealSuccess(stealScore : number){
        this.RemoveBubble();
        let initPoint = new laya.maths.Point(this.imgField.width/3, -70);
        initPoint = ComFun.pointChange(this.imgField, Game.viewFarm, initPoint);
        Game.gainAward.playAni(initPoint.x, initPoint.y, Game.viewFarm.btnHome.x, Game.viewFarm.btnHome.y,Game.viewFarm, "item/jifen.png", false);
       
        let objPrompt = Laya.Pool.getItemByClass("prompt", Prompt);
        objPrompt.Show("偷走好友 "+stealScore+" 积分");
    }

    //获得灾害
    AddDisaster(...args) {
        let fieldData : any;
        if (args.length > 0) {
            fieldData = args[0];
        }
        else {
            fieldData = FieldModel.GetFieldData(this.index);
        }

        if (fieldData.disasterId == 1) {
            this.imgField.graphics.clear();
            let fieldUrl = "main/" + Cfg.DisasterCfg[1].skin + ".png";
            this.imgField.loadImage(fieldUrl);
            this.strBubbleName = Cfg.DisasterCfg[1].skin;
        }
        else if (fieldData.disasterId > 1) {
            this.AddBubble(Cfg.DisasterCfg[fieldData.disasterId].skin);
        }
    }

    //清除灾害
    ClearDisaster() {
        if (this.strBubbleName && this.strBubbleName != "harvest") {
            if (this.strBubbleName == Cfg.DisasterCfg[1].skin) {
                this.imgField.graphics.clear();
                this.imgField.loadImage("main/field1.png");
            }
            else {
                this.RemoveBubble();
            }
            this.strBubbleName = "";
        }
    }

    //显示地块的作物数据
    showCropTips(imgField : Laya.Image, index : number){
        let point = imgField.localToGlobal(new laya.maths.Point(0,0));
        Game.viewFarm.showCropTips(point, index);
    }

    //显示施肥气泡
    showFertilize(propId,stage){
        //隐藏原有气泡
        this.imgBubble.visible = false;
        if(stage > Cfg.ItemCfg[this.cropId]["stage"]){
            if(Cfg.ItemCfg[propId]["effect_type"] == Cfg.ItemCfg["prop_effect_time"]["value"]) return;
        }
        this.propBubble.visible = true;
        //增加施肥气泡
        this.propBubble.offAll(Laya.Event.CLICK);
        this.propBubbleType.graphics.clear();
        this.propBubble.graphics.clear();
        this.propBubble.loadImage("main/bubble.png");
        this.propBubble.on(Laya.Event.CLICK, this, this.haveFertilize, [propId]);

        let cropX = Math.floor(this.imgField.width/2);
        let cropY = -70;
        this.propBubble.anchorX = 0.5;
        this.propBubble.anchorY = 0.5;
        this.propBubble.pos(cropX, cropY);
        this.imgField.addChild(this.propBubble);
        let bubbleTypeUrl = "item/" + Cfg.ItemCfg[propId]["skin"] + ".png";
        let cropX2 = Math.floor(this.propBubble.width/2);
        let cropY2 = Math.floor(this.propBubble.height/2);
        this.propBubbleType.anchorX = 0.5;
        this.propBubbleType.anchorY = 0.5;
        this.propBubbleType.pos(cropX2, cropY2);
        this.propBubbleType.loadImage(bubbleTypeUrl, 0, 0, this.propBubble.width/3 * 2, this.propBubble.height/3 * 2);
        this.propBubble.addChild(this.propBubbleType);
    }

    //进行施肥操作
    haveFertilize(propId){
        let item = ComFun.GetArrayValue(KBEngine.app.player().itemList,"itemId",propId);
        if(item && item["number"]>0){
            CtrlManager.GetCtrl(CtrlNames.Warehouse).haveProp(propId, 1, this.index);
        }else{
            Game.viewFarm.hideFertilize();
        }
    }

    //进行施肥操作,服务器返回
    onHaveFertilize(propId, count){
        this.propBubble.visible = false;
        let point = ComFun.pointChange(this.propBubble,Game.viewFarm.boxGround,new laya.maths.Point(0, 0));
        Game.viewFarm.fertilize.pos(point.x, point.y);
        Game.viewFarm.fertilize_ani.play(0,false);
        CtrlManager.GetCtrl(CtrlNames.Warehouse).ChangeSeedData(propId, -count);
    }

    //隐藏施肥气泡
    hideFertilize(){
        this.imgBubble.visible = true;
        this.propBubble.visible = false;
    }

    //显示边框
    ShowFrame() {
        if (!this.imgFrame.visible) {
            this.imgFrame.loadImage("main/fieldframe.png", 0, 0);
            this.imgField.addChild(this.imgFrame);
            this.imgFrame.visible = true;
        }
    }

    //隐藏边框
    HideFrame() {
        this.imgFrame.visible = false;
    }

    //点击地块
    OnFieldClick() {
        //如果是本人农场、空地块，则设置为选中状态
        if (PlayerModel.IsSelfFarm()) {
            if (!this.cropId || this.cropId <= 0) {
                CtrlManager.GetCtrl(CtrlNames.Field).SetSelected(this.index);
                this.ShowFrame();
            }
        }
    }
}